#include "filter1.h"

static AVFormatContext *fmt_ctx = NULL;
static AVCodec *video_codec = NULL;
static AVCodecParameters *video_codec_par = NULL;
static AVCodecContext *video_codec_ctx = NULL;
static int video_stream_index = -1;

static int open_input_file(const char *path)
{

    int ret = 0;
    ret = avformat_open_input(&fmt_ctx, path, NULL, NULL);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avformat_open_input faile path=%s", path);
        return ret;
    }

    ret = avformat_find_stream_info(fmt_ctx, NULL);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avformat_find_stream_info faile ");
        return ret;
    }

    av_dump_format(fmt_ctx, 0, path, 0);
    video_stream_index = av_find_best_stream(fmt_ctx,
                                             AVMEDIA_TYPE_VIDEO,
                                             -1,
                                             -1,
                                             &video_codec,
                                             -1);

    if (video_stream_index < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "av_find_best_stream faile ");
        ret = -1;
        return ret;
    }

    video_codec_par = fmt_ctx->streams[video_stream_index]->codecpar;

    video_codec_ctx = avcodec_alloc_context3(video_codec);
    ret = avcodec_parameters_to_context(video_codec_ctx, video_codec_par);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avcodec_parameters_to_context faile ");
        return ret;
    }

    ret = avcodec_open2(video_codec_ctx, video_codec, NULL);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avcodec_open2 faile ");
        return ret;
    }

    return ret;
}

static int init_filters(const char *filter_desc)
{
    int ret = -1;
    //创建Graph
    AVFilterGraph *graph = NULL;
    AVFilterInOut *input = NULL;
    AVFilterInOut *output = NULL;
    AVFilterContext *buffer_filter_ctx = NULL;
    AVFilterContext *buffer_shink_filter_ctx = NULL;
    char args[512] = {};
    // "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d"
    AVRational time_base = fmt_ctx->streams[video_stream_index]->time_base;

    //第一步创建graph
    graph = avfilter_graph_alloc();
    if (!graph)
    {
        av_log(NULL, AV_LOG_ERROR, "avfilter_graph_alloc fail");
        goto __INIT_FILTER_ERROR;
    }

    //第二步创建buffer_filter_ctx
    const AVFilter *bufsrc = avfilter_get_by_name("buffer");
    if (!bufsrc)
    {
        av_log(NULL, AV_LOG_ERROR, "avfilter_get_by_name fail");
        goto __INIT_FILTER_ERROR;
    }

    //args 来源于ffmpeg -h filter=buffer
    snprintf(args,
             512,
             "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
             video_codec_par->width,
             video_codec_par->height,
             video_codec_par->format,
             time_base.num,
             time_base.den,
             video_codec_ctx->sample_aspect_ratio.num,
             video_codec_ctx->sample_aspect_ratio.den);

    ret = avfilter_graph_create_filter(&buffer_filter_ctx, bufsrc, "in", args, NULL, graph);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avfilter_graph_create_filter buffer_filter_ctx %s", av_err2str(ret));
        goto __INIT_FILTER_ERROR;
    }


    //第三步创建buffersink filter
    const AVFilter *bufsink = avfilter_get_by_name("buffersink");
    if (!bufsink)
    {
        av_log(NULL, AV_LOG_ERROR, "creater bufsink fail");
        goto __INIT_FILTER_ERROR;
    }
    ret = avfilter_graph_create_filter(&buffer_shink_filter_ctx, bufsink, "out", NULL, NULL, graph);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avfilter_graph_create_filter buffer_shink_filter_ctx %s", av_err2str(ret));
        goto __INIT_FILTER_ERROR;
    }
    //buffer shink  ffmpeg -h filter=buffersink
    enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_YUV420P,
                                     AV_PIX_FMT_GRAY8,
                                     AV_PIX_FMT_NONE};
    av_opt_set_int_list(buffer_shink_filter_ctx, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);



    //第四步 创建avfilter inout链表

    input = avfilter_inout_alloc();
    output = avfilter_inout_alloc();
    if (!input || !output)
    {
        av_log(NULL, AV_LOG_ERROR, "avfilter_inout_alloc fail");
        goto __INIT_FILTER_ERROR;
    }
    input->name = av_strdup("out"); 
    input->filter_ctx = buffer_shink_filter_ctx;
    input->pad_idx = 0; 
    input->next = NULL;
    output->name = av_strdup("in"); 
    output->filter_ctx = buffer_filter_ctx;
    output->pad_idx = 0; 
    output->next = NULL;

    //第五步 分析filter描述符，并构建AVFilterGraph
    ret = avfilter_graph_parse_ptr(graph, filter_desc, &input, &output, NULL);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avfilter_graph_parse_ptr %s", av_err2str(ret));
        goto __INIT_FILTER_ERROR;
    }

    //第六步 使构建好的AVFilterGraph生效
    ret = avfilter_graph_config(graph, NULL);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avfilter_graph_config %s", av_err2str(ret));
        goto __INIT_FILTER_ERROR;
    }
    return 0;

__INIT_FILTER_ERROR:
    if (graph)
    {
        avfilter_graph_free(&graph);
    }
    if (input)
    {
        avfilter_inout_free(&input);
    }
    if (output)
    {
        avfilter_inout_free(&output);
    }

    if(buffer_filter_ctx){
        avfilter_free(buffer_filter_ctx);
    }

    if(buffer_shink_filter_ctx){
        avfilter_free(buffer_shink_filter_ctx);
    }

    if(buffer_shink_filter_ctx){
        avfilter_free(buffer_shink_filter_ctx);
    }
    return ret;
}

int main(int argc, char *argv[])
{
    int ret = 0;
    // const char *filter_desc = "drawbox=30:10:64:64:red";
    const char *filter_desc="drawbox=x=30:y=10:w=64:h=64:c=red";

    const char *path = "/Users/yuanxuzhen/study/ffmpeg_study/ffmpeg/resource/in_audio.mp4";
    av_log_set_level(AV_LOG_INFO);

    ret = open_input_file(path);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "open_input_file fail");
        goto __ERROR;
    }

    ret = init_filters(filter_desc);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "init_filters fail");
        goto __ERROR;
    }

__ERROR:
    if (video_codec_ctx)
    {
        avcodec_close(video_codec_ctx);
        avcodec_free_context(&video_codec_ctx);
    }

    if (fmt_ctx)
    {
        avformat_close_input(&fmt_ctx);
        avformat_free_context(fmt_ctx);
    }

    return ret;
}
